package com.shell.customer.base.rx;

import com.shell.bee.acct.account.util.VirtualAcctUtil;
import com.shell.bee.base.utils.SpringContextUtil;
import com.shell.bee.base.utils.StringUtil;
import com.shell.customer.base.CusEvent;
import com.shell.customer.base.cmd.CIBBankVirtualAcctCmd;
import com.shell.customer.base.cmd.DefaultBankVirtualAcctCmd;
import com.shell.customer.base.vo.CusRecVo;
import com.shell.customer.po.CusAcct;
import com.shell.customer.trade.service.CustomerAcctService;
import com.shell.customer.util.CustomerUtil;
import com.shell.framework.command.ChainBase;
import com.shell.framework.command.CommandContext;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class OpenCusAcctForEditSubscriber extends OnErrorNotImplementedSubscriber<CusEvent> {
	@Autowired
	private CustomerAcctService customerAcctService;
	@Autowired
	private CIBBankVirtualAcctCmd cibBankVirtualAcctCmd;
	@Autowired
	private DefaultBankVirtualAcctCmd defaultBankVirtualAcctCmd;

	public void onApplicationEvent(CusEvent event) {
		CusRecVo cusRec = event.getCusRecVo();

		CusAcct cusAcct = event.getCusAcct();
		CusAcct delAcct = event.getDelCusAcct();

		String realAcctUseStr = SpringContextUtil.getProperty("real.acct.use");
		String[] realAcctUses = StringUtil.emptyAndNull(realAcctUseStr) ? null : realAcctUseStr.split(",");
		if (realAcctUses == null) {
			realAcctUses = new String[1];
			realAcctUses[0] = "00";
		}
		this.customerAcctService.addCard(cusAcct, cusRec, realAcctUses);
		if ((!cusRec.isOperator()) && (!cusRec.isArtisan()) && (CustomerUtil.onlinePay())) {
			if (VirtualAcctUtil.onVirtual()) {
				CommandContext<CusRecVo> context = new CommandContext();
				context.setSource(cusRec);
				ChainBase<CusRecVo> chainBase = new ChainBase();
				chainBase.add(this.defaultBankVirtualAcctCmd);
				chainBase.add(this.cibBankVirtualAcctCmd);
				chainBase.execute(context);
			}
		}
		this.customerAcctService.openChainVitualAcct(null, cusRec);
		if (delAcct != null) {
			this.customerAcctService.delAcctCard(delAcct);
		}
	}
}
